<?php

/**
 * Created by IntelliJ IDEA.
 * User: jimmyhsu
 * Date: 2017/4/3
 * Time: 17:31
 */
class facepp
{
    const api_key = "WRh8U08NX63ZA7DQa89BnLglHY8Go4sO";
    const api_secret = "F69nUJ86CP7Ob94X9xYIlPod9R8wy4Et";
    const faceset_all = "540f959db75ebaca76478ba8015a6a75";
    const add_faceset_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/create";
    const add_face_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/addface";
    const remove_faceset_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/delete";
    const remove_face_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface";

    public function remove_face_from_faceset($faceset_token, $face_token)
    {
        $post_data = array();
        $post_data['api_key'] = $this::api_key;
        $post_data['api_secret'] = $this::api_secret;
        $post_data['faceset_token'] = $faceset_token;
        $post_data['face_tokens'] = $face_token;
        return $this::post($post_data, $this::remove_face_url);
    }

    public function add_faceset($outer_id, $display_name=null)
    {
        $outer_id .= "1";
        $post_data = array();
        $post_data['api_key'] = $this::api_key;
        $post_data['api_secret'] = $this::api_secret;
        if ($display_name != null) {
            $post_data['display_name'] = $display_name;
        }
        $post_data['outer_id'] = $outer_id;
        return $this::post($post_data, $this::add_faceset_url);
    }

    public function add_face($faceset_token, $face_tokens)
    {
        $post_data = array();
        $post_data['api_key'] = $this::api_key;
        $post_data['api_secret'] = $this::api_secret;
        $post_data['faceset_token'] = $faceset_token;
        $post_data['face_tokens'] = $face_tokens;
        return $this::post($post_data, $this::add_face_url);
    }

    public function remove_faceset_by_token($faceset_token, $check_empty = 1)
    {
        $post_data = array();
        $post_data['api_key'] = $this::api_key;
        $post_data['api_secret'] = $this::api_secret;
        $post_data['faceset_token'] = $faceset_token;
        if ($check_empty == 0) {
            $post_data['check_empty'] = $check_empty;
        }
        return $this::post($post_data, $this::remove_faceset_url);
    }

    public function remove_faceset_by_id($outer_id, $check_empty = 1)
    {
        $post_data = array();
        $post_data['api_key'] = $this::api_key;
        $post_data['api_secret'] = $this::api_secret;
        $post_data['outer_id'] = $outer_id."1";
        if ($check_empty == 0) {
            $post_data['check_empty'] = $check_empty;
        }
        return $this::post($post_data, $this::remove_faceset_url);
    }

    private function post($post_data, $url)
    {
        $o = "";
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $post_data = substr($o, 0, -1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        $json = json_decode($result, true);
        return $json;
    }
}